package org.gitman.agentplatform.ui.simulation;

import javax.swing.JTextField;

public class RangedTextField extends JTextField{
	private Integer min;
	private Integer max;
	
	public RangedTextField(Integer min, Integer max) {
		super();
		setRange(min, max);
	}
	public RangedTextField(Integer min, Integer max, int size) {
		super(size);
		setRange(min, max);
	}
	public void setRange(Integer min, Integer max){
		this.min = min;
		this.max = max;
		if(isRange()){
			setText("(" + min.toString() + ";" + (max == Integer.MAX_VALUE? "inf" : max.toString()) + ")");
		}else{
			setText(min == Integer.MAX_VALUE ? "inf" :min.toString());
		}
	}	
	public boolean isRange(){
		return !min.equals(max);
	}
	public Integer getMin() {
		return min;
	}
	public void setMin(Integer min) {
		this.min = min;
	}
	public Integer getMax() {
		return max;
	}
	public void setMax(Integer max) {
		this.max = max;
	}
	public void parseText() throws NumberFormatException{
		String text = getText().trim().toLowerCase();
		if(text.contains(";")){
			text = text.replaceAll("\\(|\\)", "");
			String[] tab = text.split(";");
			if(tab[0].equals("inf")){
				min = Integer.MAX_VALUE;
			}else{
				min = Integer.parseInt(tab[0]);
			}
			if(tab[1].equals("inf")){
				max = Integer.MAX_VALUE;
			}else{
				max = Integer.parseInt(tab[1]);
			}
		}else{
			if(text.equals("inf")){
				min = max = Integer.MAX_VALUE;
			}else{
				min = max = Integer.parseInt(text);
			}
		}
	}
}
